package org.example.myframework.frame.common.redis.init;

import lombok.extern.slf4j.Slf4j;
import org.example.myframework.frame.common.redis.utils.SpringContextUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class InitCache implements CommandLineRunner {


    /**
     * 项目启动之后的操作
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        log.info("加载【Redis】初始化缓存数据...............................");

        // 调用init方法进行预热
        ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
        // 获取当前抽象类下继承的所有的类
        Map<String, AbstractCache> beanMap = applicationContext.getBeansOfType(AbstractCache.class);
        if (beanMap.isEmpty()) {
            return;
        }
        for (Map.Entry<String, AbstractCache> entry : beanMap.entrySet()) {
            AbstractCache abstractCache = (AbstractCache) SpringContextUtil.getBean(entry.getValue().getClass());
            // 获取子类，并对cache进行初始化
            abstractCache.initCache();
        }
    }
}
